<template>
  <!-- 借出 -->
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    :confirmLoading="confirmLoading"
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <j-form-container>
        <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
          
          <a-form-model-item label="借出数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lendNum">
            <a-input-number v-model="model.lendNum" placeholder="请输入借出数量" style="width: 100%" />
          </a-form-model-item>
        
          <a-form-model-item label="借出人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lendUser">
            <a-input v-model="model.lendUser" placeholder="请输入借出人" style="width: 100%" />
          </a-form-model-item>
        
          <a-form-model-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phone">
            <a-input v-model="model.phone" placeholder="请输入联系电话" style="width: 100%" />
          </a-form-model-item>
        
          <a-form-model-item label="借出时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lendTime">
            <a-date-picker
              showTime
              value-format="YYYY-MM-DD HH:mm:ss"
              format="YYYY-MM-DD HH:mm:ss"
              placeholder="请选择借出时间"
              v-model="model.lendTime"
              :disabledDate="disabledAfterDate"
              style="width:100%;"
            />
          </a-form-model-item>
        
          <a-form-model-item
            label="计划归还时间"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="returnPlanTime"
          >
            <a-date-picker
              showTime
              value-format="YYYY-MM-DD HH:mm:ss"
              format="YYYY-MM-DD HH:mm:ss"
              placeholder="请选择计划归还时间"
              v-model="model.returnPlanTime"
              :disabledDate="disabledBeforeDate"
              style="width:100%;"
            />
          </a-form-model-item>
            
        </a-form-model>
      </j-form-container>
    </a-spin>
  </j-modal>
</template>
<script>
import moment from 'moment'
import { httpAction, getAction } from '@/api/manage'
import { validateMobile } from '@/utils/rules.js'
export default {
  name: 'SkCirculateModel',
  data() {
    return {
      title: '借出',
      width: 600,
      visible: false,
      disableSubmit: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        lendNum: [{ required: true, message: '请输入借出数量!' }, { validator: this.validateCanLentNum }],
        phone: [{ required: true, message: '请输入联系电话!' }, { validator: validateMobile }],
        lendUser: [{ required: true, message: '请输入借出人!' }],
        lendTime: [{ required: true, message: '请选择借出时间!' }],
        returnPlanTime: [{ required: true, message: '请选择计划归还时间!' }],
        status: [{ required: true, message: '请输入状态!' }]
      },
      url: {
        add: '/management/skToolLendRecord/add',
        edit: '/management/skToolLendRecord/edit'
      },
      addFlag: false,
      canLentNum: 0
    }
  },
  components: {},

  filters: {},

  computed: {},

  created() {
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },

  activated() {},

  mounted() {},

  methods: {
    // 禁用今天之后的日期
    disabledAfterDate(current) {
      return current && current > moment().endOf('day')
    },
    // 禁用今天之前的日期
    disabledBeforeDate(current) {
      return current && current < moment().subtract(1, 'days')
    },
    // 借出id为主列表id
    add(record, flag) {
      this.canLentNum = record.canLentNum
      this.addFlag = flag
      this.model.toolId = record.id
      this.visible = true
    },
    //编辑为借还记录列表id
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    // 可借数量判断
    validateCanLentNum(rule, value, callback) {
      if (value <= this.canLentNum) {
        callback()
      } else {
        callback('当前教具可借数量为' + this.canLentNum)
      }
    },
    close() {
      this.$refs.form.resetFields()
      this.visible = false
    },
    handleOk() {
      const that = this
      console.log(this.model)
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (this.addFlag) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.close()
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    submitCallback() {
      this.$emit('ok')
      this.visible = false
    },
    handleCancel() {
      this.close()
    }
  }
}
</script>
<style scoped>
/* @import url(); 引入css类 */
</style>
